package com.example.firstboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.firstboot.entity.TbHistory;
import com.example.firstboot.entity.selectPAndD;
import com.example.firstboot.service.TbHistoryService;
import com.example.firstboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    private TbHistoryService tbHistoryService;

    /**
     * 获取tb_history表中的所有信息
     * @return
     */
    @RequestMapping("/findAllHistory")
    @ResponseBody
    public JsonResult<TbHistory> findAllHistory(){
        List<TbHistory> list=tbHistoryService.findAllHistory();
//        System.out.println(list);
        if (list.size()<0){
            return new JsonResult(-1,"请求失败，没有查询到门诊医生信息！");
        }else {
            return new JsonResult(list,"请求成功");
        }
    }

    @PostMapping("/findHistoryByAccount")
    @ResponseBody
    public JsonResult findHistoryByAccount(@RequestBody JSONObject json){
        String account= (String) json.get("account");
        List<TbHistory> list=tbHistoryService.findHistoryByAccount(account);
//        System.out.println(list);
        if (list.size()<0){
            return new JsonResult(-1,"请求失败，没有查询到门诊医生信息！");
        }else {
            return new JsonResult(list,"请求成功");
        }
    }

    @PostMapping("/findHistoryByPAccount")
    @ResponseBody
    public JsonResult findHistoryByPAccount(@RequestBody JSONObject json){
        String paccount= (String) json.get("paccount");
        System.out.println("==========="+paccount);
        List<TbHistory> list=tbHistoryService.findHistoryByAccount(paccount);
//        System.out.println(list);
        if (list.size()<0){
            return new JsonResult(-1,"请求失败，没有查询到门诊医生信息！");
        }else {
            return new JsonResult(list,"请求成功");
        }
    }

    /**
     * 获取tb_history表中的所有信息
     * @return
     */
    @PostMapping("/selectPAndD")
    @ResponseBody
    public JsonResult selectPAndD(@RequestBody JSONObject json){
        String daccount= (String) json.get("daccount");
        List<selectPAndD> list=tbHistoryService.selectPAndD(daccount);
//        System.out.println("================"+list);
        if (list.size()<0){
            return new JsonResult(-1,"请求失败，没有查询到门诊医生信息！");
        }else {
            return new JsonResult(list,"请求成功");
        }
    }

    @RequestMapping("/searchHistory")
    @ResponseBody
    public JsonResult searchHistory(@RequestBody JSONObject json){
        String illness_description=String.valueOf(json.get("illness_description"));
        List<TbHistory> tbHistory= tbHistoryService.searchHistory(illness_description);
//        System.out.println(tbHistory);
        if(tbHistory==null){
            return new JsonResult(-1,"请求失败，没有查询到相应的病历！");
        }else{
            return new JsonResult(tbHistory,"请求成功");
        }
    }

    /**
     * 医生端新增问诊
     * @param json
     * @return
     */
    @PostMapping("/saveHistory")
    @ResponseBody
    public JsonResult<TbHistory> saveHistory(@RequestBody JSONObject json){
        String paccount= (String) json.get("paccount");
        String illness_description= (String) json.get("illness_description");
        String check_id= (String) json.get("check_id");
        TbHistory tbHistory=new TbHistory();
        tbHistory.setPaccount(paccount);
        tbHistory.setIllness_description(illness_description);
        tbHistory.setCheck_id(check_id);
//        System.out.println(tbHistory);
        int i=tbHistoryService.saveHistorys(tbHistory);
        if(i>0){
            return new JsonResult(i);
        }else{
            return new JsonResult<TbHistory>(-1,"请求失败");
        }
    }

    /**
     * 医生端开设药方
     */
    @PostMapping("/updateHistory")
    @ResponseBody
    public JsonResult<TbHistory> updateHistory(@RequestBody TbHistory tbHistory){
        int i=tbHistoryService.updateHistorys(tbHistory);
//        System.out.println(tbHistory);
        if(i>0){
            return new JsonResult(i);
        }else{
            return new JsonResult<TbHistory>(-1,"请求失败");
        }
    }
}
